#!/bin/sh

# Slackware build script for atari800

# Written by B. Watson (yalhcru@gmail.com)
# Modified by the SlackBuilds.org project.

PRGNAM=atari800
VERSION=${VERSION:-2.1.0}
ARCH=${ARCH:-i486}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}

CWD=$(pwd)
TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

# Do you want the emulated serial port to listen on a TCP port?
# This is enabled by default, build with RIO_DEVICE=no to disable.
RIO_DEVICE=${RIO_DEVICE:-yes}

# Do you want the package to automatically associate itself with
# the various Atari 8-bit file formats (disk/cart/tape images)?
# Build with MIME_TYPES=no to disable
MIME_TYPES=${MIME_TYPES:-yes}

if [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
fi

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
tar xvf $CWD/$PRGNAM-$VERSION.tar.gz
cd $PRGNAM-$VERSION
chown -R root:root .
chmod -R a-s,u+w,go+r-w .

if [ "$RIO_DEVICE" = "yes" ]; then
	RIO="--enable-riodevice";
else
	RIO="--disable-riodevice";
fi

cd src
CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
	./configure \
	--target=sdl \
	--prefix=/usr \
	--enable-monitorprofile \
	--enable-seriosound \
	--enable-clipsound \
	--enable-readline \
	$RIO \
	--enable-monitorbreakpoints \
	--enable-monitortrace

make

# The Makefile ignores --datarootdir, --mandir, --docdir, so force it:
make install DESTDIR=$PKG \
	DOC_DIR=/usr/doc/$PRGNAM-$VERSION MAN_DIR=/usr/man/man1

chmod 755 $PKG/usr/bin/*
strip $PKG/usr/bin/$PRGNAM

gzip -9 $PKG/usr/man/man1/$PRGNAM.1

# Package-specific docs
for i in README_rom_images.txt $PRGNAM.SlackBuild; do
	cat $CWD/$i > $PKG/usr/doc/$PRGNAM-$VERSION/$i
done

cat $CWD/README_SBo.txt > $PKG/usr/doc/$PRGNAM-$VERSION/README_SBo.txt

# Sample code and contrib stuff, throw it in the doc dir
cp -r ../act ../util $PKG/usr/doc/$PRGNAM-$VERSION

# Link a copy of the ROM doc to the ROM dir (we really want them to read this)
mkdir -p $PKG/usr/share/$PRGNAM
( cd $PKG/usr/share/$PRGNAM && \
  ln -s ../../doc/$PRGNAM-$VERSION/README_rom_images.txt . )

mkdir -p $PKG/usr/share/{applications,pixmaps}
cat $CWD/$PRGNAM.desktop > $PKG/usr/share/applications/$PRGNAM.desktop
cp $CWD/pixmaps/* $PKG/usr/share/pixmaps/

# All these .desktop, XML, and icon files are extras, not part of the
# atari800 release. They've been submitted to the atari800 mailing list,
# but have not yet been imported into the tree.
if [ "$MIME_TYPES" = "yes" ]; then

	mkdir -p $PKG/usr/share/{mime/packages,mimelnk/application}
	cat $CWD/mime/$PRGNAM.xml > $PKG/usr/share/mime/packages/$PRGNAM.xml
	cp $CWD/mime/*.desktop $PKG/usr/share/mimelnk/application/

	# Add the MimeType stuff to the .desktop file
	cat $CWD/mime/atari800_desktop_mime >> $PKG/usr/share/applications/$PRGNAM.desktop

fi

# Optional binfmt_misc support script (admin must enable by hand)
mkdir -p $PKG/etc/rc.d
cat $CWD/rc.atari800_binfmt_misc.new \
	> $PKG/etc/rc.d/rc.atari800_binfmt_misc.new

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

cd $PKG
/sbin/makepkg -l y -c y $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.tgz
